/*============================================================================*/
/*                        Tortoise Team -- ITESO	                          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:         %HAL_RCA_Camera.c%
* Instance:         RPL_1
* %version:         1.0 %
* %created_by:      Andres Torres Garcia %
* %date_created:    Fri Oct 12 12:41:01 2012 %
*=============================================================================*/
/* DESCRIPTION : C source file for the hardware layer of the RCA Camera.      */
/*============================================================================*/
/* FUNCTION COMMENT : Contains the function, private defines and variables    */
/*					  declarations of the hardware layer of the RCA Camera    */                                                                            
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 12/10/2012 | SAR/SIF/SCN_xxx               | Andres Torres     */
/* Integration under Continuus CM                                             */
/*============================================================================*/

/* Includes */
/* -------- */
#include "MAL_ADC.h"
#include "MAL_IOAPI.h"
#include "debug.h"
#include "MAL_pit.h"
#include "HAL_RCA_Camera.h"

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 


/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 

T_SWORD sw_LinePixels[NUM_LINES_CAPTURE + 1][PIXELS_PER_LINE];

static T_UBYTE ub_CurrentLine = 0;
T_UBYTE ub_LinesCaptured = 0;

static T_UBYTE ub_CaptureLineCnt = 0;
static T_SBYTE us_pixelCnt = 0;
T_UBYTE ub_TotalPix[NUM_LINES_CAPTURE];

static T_UBYTE ub_lineSelect[LINES_OF_RESOLUTION];

T_UBYTE ub_isVertSync = 0;

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

#define MIN_PIX_TO_CONSIDERATE_LINE		30
#define LOW_PIX_HORIZONTAL_SYNC			3

/* Private functions prototypes */
/* ---------------------------- */
void vfn_readLine(void);
void vfnInitLineSelector(void);

/* Exported functions prototypes */
/* ----------------------------- */
void HAL_vfnInitRCA_Capturing(void);
void vfnGetImage(void);
void HAL_vfnSelectPredefLinesToCapture(void);

void HAL_vfnStopCapturing(void);
void HAL_vfnStartCapturing(void);
void HAL_vfnDeselectLine(T_UBYTE line);
void HAL_vfnSelectLine(T_UBYTE line);


/* Inline functions */
/* ---------------- */

/* Private functions */
/* ----------------- */
/**************************************************************
 *  Name                 : 
 *  Description          :
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :
 *  Critical/explanation :    No
 **************************************************************/

/* Exported functions */
/* ------------------ */

void HAL_vfnInitRCA_Capturing(void)
{
	vfnConfigPinAs_ADC_Channel(ANP_PCR_0);
	vfnConfig_ADC_Mode(SCAN);
	vfnSetChannels( 0, 0x1 );
	vfnSetConvertionTime( 0, FAST );
	
	vfnADC_StartConvertion();
	
	vfnInit_Adc_WatchdogL(0,0,HORIZ_SYNC_THRES);//20
	
	//vfnInit_Adc_WatchdogH(0,1,HORIZ_SYNC_THRES);//90
	vfnInit_Adc_WatchdogH(0,0,VERTI_SYNC_THRES);
	
	HAL_vfnStopCapturing();
	
	//DISABLE_WDGH_INT;
	
	vfnInitLineSelector();
	
	IO_SetPinAsOutput(DEBUG_PIN, 1);
	
	//vfnEnable_Adc_Watchdog_Channel0();
}

void vfnGetImage(void)
{	
	IO_PinWrite( DEBUG_PIN, 0);
	
	vfn_readLine();
	 
	if( us_pixelCnt < MIN_PIX_TO_CONSIDERATE_LINE || ub_CurrentLine > TOTAL_LINES)
	{
		//Vertical synchronization detected.
		
		ub_isVertSync = 1;
		
		ub_LinesCaptured = ub_CaptureLineCnt;
		
		ub_CaptureLineCnt = 0;
		ub_CurrentLine = 0;
		
		SET_HIGH_THRESH(0, VERTI_SYNC_THRES);
		
		ENABLE_WDGH_INT;
		DISABLE_WDGL_INT;
	}
	else
	{
	
		SET_HIGH_THRESH(0, HORIZ_SYNC_THRES);
		
		ENABLE_WDGH_INT;
		DISABLE_WDGL_INT;
	}
	
	IO_PinWrite( DEBUG_PIN, 1);
	
	CLEAR_HIGH_WDG_FLG;
}

void vfn_readLine(void)
{
	us_pixelCnt = -1;
	
	do
	{
		us_pixelCnt++;
	
		while (!ADCConvertionComplete(ANP_0)) {};     /* Wait for last scan to complete */
		sw_LinePixels[ub_CaptureLineCnt][us_pixelCnt] = ADCReadValue(ANP_0);     /* Read ANP0 conversion result data */
		
	}while( ( ( sw_LinePixels[ub_CaptureLineCnt][us_pixelCnt] > HORIZ_SYNC_THRES ) || us_pixelCnt < LOW_PIX_HORIZONTAL_SYNC ) && us_pixelCnt < PIXELS_PER_LINE );
	
	if(ub_lineSelect[ub_CurrentLine] == 1)
	{
		ub_TotalPix[ub_CaptureLineCnt] = us_pixelCnt;
	
		ub_CaptureLineCnt++;
	}
	
	ub_CurrentLine++;
}

void vfnInitLineSelector(void)
{
	T_UBYTE i;
	
	for(i = 0; i < LINES_OF_RESOLUTION; i++)
	{
		ub_lineSelect[i] = 0;
	}
}

void HAL_vfnSelectPredefLinesToCapture(void)
{	
	HAL_vfnSelectLine(A_c);
	HAL_vfnSelectLine(B_c);
	HAL_vfnSelectLine(C_c);
	HAL_vfnSelectLine(D_c);
	HAL_vfnSelectLine(E_c);
	HAL_vfnSelectLine(F_c);
	HAL_vfnSelectLine(G_c);
	HAL_vfnSelectLine(H_c);
}

void HAL_vfnSelectLine(T_UBYTE line)
{
	ub_lineSelect[line] = 1;
}

void HAL_vfnDeselectLine(T_UBYTE line)
{
	ub_lineSelect[line] = 0;
}

void HAL_vfnStartCapturing(void)
{
	ENABLE_WDGL_INT;
	DISABLE_WDGH_INT;
}

void HAL_vfnStopCapturing(void)
{
	DISABLE_WDGL_INT;
	DISABLE_WDGH_INT;
}
